﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Enter.DataProxy.Cache.Configuration;
using System.Threading;

namespace Enter.DataProxy.Cache
{
    internal class ErrorCache : ICacheService
    {

        #region 私有变量 装载初始化
        System.Threading.Timer timer1 = null;

        private static CacheType _Errtype = CacheType.velocitycache;

        private static int _DEFAULT_MINUTES = 60;
        private static int _DEFAULT_Max = 0;
        private static int _max = 0;
        private static string _Region = string.Empty;

        public ErrorCache()
        {
            GetCurrentCache();
        }


        private void GetCurrentCache()
        {
            CacheConfigurationManager VelocityManager = new CacheConfigurationManager();
            CacheErrSet ErrorSetting = VelocityManager.GetErrSetting();
            if (ErrorSetting.EnableCheck)
            {
                _Errtype = new CacheConfigurationManager().GetCacheSettings().Cachetype;
                _DEFAULT_MINUTES = ErrorSetting.IntervalTime * 1000;
                _DEFAULT_Max = ErrorSetting.Maximum;
                TimerCallback inPhaseDelegate = new TimerCallback(Timer_Cache);
                timer1 = new Timer(inPhaseDelegate, "", _DEFAULT_MINUTES, _DEFAULT_MINUTES);
            }
        }


        #endregion

        #region 接口
        /// <summary>
        /// 是否使用缓存
        /// </summary>
        public bool IsCache()
        {
            return false;
        }

        public void ReleaseConnection()
        {

        }
        public void DeltelCache()
        {

        }

        public T Get<T>(string key)
        {
            return Get<T>(key, _Region);
        }

        public T Get<T>(string key, string region)
        {
            return default(T);
        }

        public List<T> GetObjectsByKey<T>(List<string> key)
        {
            return null;
        }


        public Dictionary<string, T> GetKeyObjectsByKey<T>(List<string> key)
        {
            return null;
        }

        public void Remove(string key)
        {
            Remove(key, _Region);
        }

        public void Remove(string key, string region)
        {

        }

        public void Remove(List<string> key)
        {

        }


        public void Remove(List<string> key, string region)
        {

        }

        public void Put(string key, object value)
        {
            Put(key, value, 0);
        }

        public void Put(string key, object value, string region)
        {
            Put(key, value, 0);
        }


        public void Put(string key, object value, int Seconds)
        {
           // Put(key, value, Seconds, null);
        }



        public bool Exists(string key)
        {
            return Exists(key, _Region);
        }

        public bool Exists(string key, string region)
        {
            return false;
        }

        #endregion

        #region 检查缓存服务器是否恢复

        /// <summary>
        /// 缓存服务器检查服务
        /// </summary>
        private void Timer_Cache(object state)
        {
            if (_DEFAULT_Max > 0)
            {
                if (_DEFAULT_Max < _max++)
                {
                    timer1.Dispose();
                }
            }
            timer1.Change(Timeout.Infinite, Timeout.Infinite);
            if (!Check_Cache())
            {
                timer1.Change(_DEFAULT_MINUTES, _DEFAULT_MINUTES);
            }
            else
            {
                timer1.Dispose();
            }
        }


        /// <summary>
        /// 检查缓存服务器是否恢复
        /// </summary>
        private bool Check_Cache()
        {
            bool result = false;
            try
            {
                switch (_Errtype)
                {
                    case CacheType.memcache:
                        new MemCache(false);
                        break;
                    case CacheType.webcache:
                        new WebCache();
                        break;
                }
                result = true;
                CacheService.Reset();
            }
            catch
            {

            }
            return result;
        }


        #endregion
    }
}
